package com.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.it.aiDispatch.DispatchApi;
import com.it.aiReportClaim.ReportClaimApi;
import com.it.entity.aiClaimRegistration.AiClaimRegistration;
import com.it.entity.reopen.AiClaimReopen;
import com.it.entity.report.AiClaimReport;
import com.it.mapper.AiClaimReopenMapper;
import com.it.service.AiClaimReopenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.R;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lutao
 * @since 2025年09月22日
 */
@Service
public class AiClaimReopenServiceImpl extends ServiceImpl<AiClaimReopenMapper, AiClaimReopen> implements AiClaimReopenService {

    @Autowired
    private AiClaimReopenMapper aiClaimReopenMapper;

    @Autowired
    private ReportClaimApi reportClaimApi;

    @Override
    public R add(AiClaimReopen aiClaimReopen) {

        LambdaQueryWrapper<AiClaimReopen> queryWrapper = new LambdaQueryWrapper<>();

        if(aiClaimReopen.getReportNo()!=null && !aiClaimReopen.getReportNo().isEmpty()){
            queryWrapper.eq(AiClaimReopen::getReportNo,aiClaimReopen.getReportNo());
        }


        List<AiClaimReopen> list = aiClaimReopenMapper.selectList(queryWrapper);

        for(AiClaimReopen aiClaimReopen1 : list) {

            if (aiClaimReopen1 != null && aiClaimReopen1.getApproveStatus() != 3) {
                return R.Failed("已申请过重开赔案");
            }
        }

        aiClaimReopen.setApproveStatus(1);
        return R.Success("添加成功",aiClaimReopenMapper.insert(aiClaimReopen));
    }

    @Override
    public R selectReopenList(int pageNum, int pageSize, String reportNo, String policyId,Integer approveStatus) {
        PageHelper.startPage(pageNum, pageSize);

        LambdaQueryWrapper<AiClaimReopen> queryWrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(reportNo)){
            queryWrapper.like(AiClaimReopen::getReportNo,reportNo);
        }
        if(StringUtils.isNotBlank(policyId)){
            queryWrapper.like(AiClaimReopen::getPolicyId,policyId);
        }
        if(approveStatus!=null){
            queryWrapper.eq(AiClaimReopen::getApproveStatus,approveStatus);
        }

        List<AiClaimReopen> aiClaimReopenList = aiClaimReopenMapper.selectList(queryWrapper);
        return R.Success("查询成功",aiClaimReopenList);
    }

    @Override
    @Transactional
    public R updateByReportNo(AiClaimReopen aiClaimReopen) {

        AiClaimReopen aiClaimReopen1 = aiClaimReopenMapper.selectById(aiClaimReopen.getReopenId());
        aiClaimReopen1.setApproveStatus(aiClaimReopen.getApproveStatus());
        aiClaimReopen1.setApproveRemark(aiClaimReopen.getApproveRemark());
        aiClaimReopen1.setApproveUser(aiClaimReopen.getApproveUser());
        if(aiClaimReopen1.getApproveStatus() == 2){
            AiClaimReport aiClaimReport =  reportClaimApi.selectOne(aiClaimReopen1.getReportNo());
            aiClaimReport.setId(null);
            AiClaimReport aiClaimReport1 = reportClaimApi.add(aiClaimReport,aiClaimReopen1.getReopenMoney());
            aiClaimReopen1.setNewReportId(aiClaimReport1.getReportNo());
            reportClaimApi.updateStatus(aiClaimReopen.getReportNo(),11);
        }

        return R.Success("已审查",aiClaimReopenMapper.updateById(aiClaimReopen1));
    }


}
